package ru.mcfr.oxygen.framework.ui;

import ro.sync.ecss.extensions.api.AuthorAccess;

import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Popup window used to display Simple Documentation Framework specific information.
 */
public class MOFPopupWindow extends JWindow {

    /**
     * Text area used to display useful informations.
     */
    private JTextArea infoTextArea;

    /**
     * Access to the author specific functions.
     */
    AuthorAccess authorAccess;

    /**
     * The display time of the popup window (seconds).
     */
    private int timeToDisplay;

    /**
     * @param access          Author access.
     * @param infoDescription Description.
     */
    public MOFPopupWindow(AuthorAccess access, String infoDescription) {
        super((JFrame) access.getWorkspaceAccess().getParentFrame());
        this.authorAccess = access;

        // Create information text area.
        infoTextArea = new JTextArea();
        infoTextArea.setLineWrap(false);
        infoTextArea.setWrapStyleWord(false);
        infoTextArea.setEditable(false);
        infoTextArea.setFocusable(false);

        JPanel mainContent = new JPanel(new BorderLayout());
        if (infoDescription != null) {
            mainContent.add(new JLabel(infoDescription), BorderLayout.NORTH);
        }
        mainContent.setFocusable(false);
        mainContent.add(infoTextArea, BorderLayout.SOUTH);
        mainContent.setBorder(BorderFactory.createLineBorder(Color.black));
        getContentPane().add(mainContent);
        setVisible(false);
    }

    /**
     * Set the time to display this popup window.
     *
     * @param timeToDisplay The display time in seconds.
     */
    public void setTimeToDisplay(int timeToDisplay) {
        this.timeToDisplay = timeToDisplay;
    }

    /**
     * Display the specified text information.
     *
     * @param text  The text to be displayed in the popup window.
     * @param relX  The "x" coordinate relative to the viewport.
     * @param relY  The "y" coordinate relative to the viewport.
     * @param delta The translation point where the popup should be displayed from the given (x, y) point.
     */
    public void display(String text, int relX, int relY, int delta) {
        // Transform the given relative coordinates into absolute coordinates.
        try {
            int[] translatedPoint = authorAccess.getEditorAccess().getLocationOnScreen(relX, relY);
            setVisible(false);
            infoTextArea.setText(text);
            setLocation(translatedPoint[0] + delta, translatedPoint[1] + delta);
            pack();
            // Show the information popup window
            setVisible(true);

            // Hide the window when the given display time is finished.
            if (timeToDisplay > 0) {
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {

                    public void run() {
                        setVisible(false);
                    }
                }, timeToDisplay * 1000);
            }
        } catch (IllegalComponentStateException e) {
            // Do nothing
        }
    }
}
